package com.initial.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.initial.entity.*;
import com.initial.mapper.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Contract {
    @Resource
    private TbCrmContractMapper tbCrmContractMapper;
    @Resource
    private TbCrmBusinessMapper tbCrmBusinessMapper;
    @Resource
    private TbCrmCustomerMapper tbCrmCustomerMapper;
    @Resource
    private TbSystemUserMapper tbSystemUserMapper;
    @Resource
    private TbCrmContactsMapper tbCrmContactsMapper;

    //去合同模块
    @RequestMapping("/toContract")
    public String toContract() {
        return "views/manage/contract/index_contract";
    }

    //去合同展示页面
    @RequestMapping("/toListContract")
    public String toListContract() {
        return "views/manage/contract/list_contract";
    }

    //前端请求  数据返回前端
    @RequestMapping("/getContract")
    @ResponseBody
    public Map getContract(Integer page, Integer rows, HttpSession session) {
        Map map = new HashMap();
        String filter = (String) session.getAttribute("filter");
        String likeOrAccurate = (String) session.getAttribute("likeOrAccurate");
        String search = (String) session.getAttribute("search");
        /*System.out.println("filter:" + filter);
        System.out.println("search:" + search);*/
        String customer = null;
        String contact = null;
        String principal = null;
        String like = null;
        String accurate = null;
        if ("customer".equals(filter)) {
            customer = search;
        }
        if ("contact".equals(filter)) {
            contact = search;
        }
        if ("principal".equals(filter)) {
            principal = search;
        }
        /*if ("like".equals(likeOrAccurate)) {
            //模糊
        }*/
        /*if ("accurate".equals(likeOrAccurate)) {
            //精确
        }*/
        /*System.out.println("cust:" + customer);
        System.out.println("con:" + contact);
        System.out.println("pri:" + principal);*/
        IPage<TbCrmContract> tbCrmContractIPage = tbCrmContractMapper.selectAll(customer, contact, principal, new Page<>(page, rows));
//        tbCrmContractIPage.getRecords().forEach(tbCrmContract -> System.out.println(tbCrmContract));
        map.put("total", tbCrmContractIPage.getTotal());
        map.put("rows", tbCrmContractIPage.getRecords());
        session.removeAttribute("filter");
        session.removeAttribute("likeOrAccurate");
        session.removeAttribute("search");
        return map;
    }

    //模糊查询
    @RequestMapping("/searchContract")
    public String searchContract(HttpSession session, String filter, String likeOrAccurate, String search) {
        session.setAttribute("filter", filter);
        session.setAttribute("likeOrAccurate", likeOrAccurate);   //把查询条件和筛选条件放入session
        session.setAttribute("search", search);
        return "views/manage/contract/list_contract"; //返回展示页面
    }

    //查看
    @RequestMapping("/seeContract")
    public String seeContract(Integer id, ModelMap modelMap) {
//        ModelMap modelMap = new ModelMap();
        System.out.println(id);
        TbCrmContract tbCrmContract = tbCrmContractMapper.selectById(id);
        System.out.println(tbCrmContract);
        modelMap.addAttribute("contractVO", tbCrmContract);
        return "views/manage/contract/see_contract";
    }

    //去修改界面
    @RequestMapping("/toEditContract")
    public String toEditContract(Integer id, ModelMap modelMap) {
        System.out.println(id);
        TbCrmContract tbCrmContract = tbCrmContractMapper.selectById(id);
        modelMap.addAttribute("contractVO", tbCrmContract);
        List<TbCrmBusiness> tbCrmBusinesses = tbCrmBusinessMapper.selectName();
        modelMap.addAttribute("tbCrmBusinesses", tbCrmBusinesses);
        return "views/manage/contract/edit_contract";
    }

    //修改
    @RequestMapping("/editContract")
    @ResponseBody
    public Map editContract(Integer id, TbCrmContract tbCrmContract, TbCrmContacts tbCrmContacts, TbCrmCustomer tbCrmCustomer, TbSystemUser tbSystemUser) {
        Map map = new HashMap();
        int i1 = tbCrmContractMapper.updateById(tbCrmContract);
        int i2 = tbCrmContactsMapper.updateById(tbCrmContacts);
        int i3 = tbCrmCustomerMapper.updateById(tbCrmCustomer);
        int i4 = tbSystemUserMapper.updateById(tbSystemUser);
        if (i1 == 1 && i2 == 1 && i3 == 1 && i4 == 1) {
            map.put("success", true);
            map.put("message", "修改成功");
        } else {
            map.put("success", false);
            map.put("message", "修改失败");
        }
        return map;
    }

    //去添加界面
    @RequestMapping("/toAddContract")
    public String toAddContract(ModelMap modelMap) {
        List<TbCrmBusiness> tbCrmBusinesses = tbCrmBusinessMapper.selectName();
        modelMap.addAttribute("tbCrmBusinesses", tbCrmBusinesses);
        return "views/manage/contract/add_contract";
    }

    //添加
    @RequestMapping("/addContract")
    @ResponseBody
    public Map addContract(TbCrmContract tbCrmContract, TbCrmContacts tbCrmContacts, TbCrmCustomer tbCrmCustomer, TbSystemUser tbSystemUser) {
        Map map = new HashMap();
        int i1 = tbCrmContractMapper.insert(tbCrmContract);
        int i2 = tbCrmContactsMapper.insert(tbCrmContacts);
        int i3 = tbCrmCustomerMapper.insert(tbCrmCustomer);
        int i4 = tbSystemUserMapper.insert(tbSystemUser);
        if (i1 == 1 && i2 == 1 && i3 == 1 && i4 == 1) {
            map.put("success", true);
            map.put("message", "添加成功");
        } else {
            map.put("success", false);
            map.put("message", "添加失败");
        }
        return map;
    }

    //删除
    @RequestMapping("/deleteContract")
    @ResponseBody
    public Map deleteContract(Integer ids) {
        Map map = new HashMap();
        System.out.println(ids);
        int i = tbCrmContractMapper.deleteById(ids);
        if (i == 1) {
            map.put("success", true);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("message", "删除失败");
        }
        return map;
    }
}
